package cn.scxy.bookcity.service.impl;

import cn.scxy.bookcity.ex.ServiceException;
import cn.scxy.bookcity.mapper.CommentMapper;
import cn.scxy.bookcity.mapper.GoodsMapper;
import cn.scxy.bookcity.mapper.UserMapper;
import cn.scxy.bookcity.pojo.dto.CommentDTO;
import cn.scxy.bookcity.pojo.entity.Comment;
import cn.scxy.bookcity.pojo.vo.CommentListItemVO;
import cn.scxy.bookcity.service.ICommentService;
import cn.scxy.bookcity.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void AddNew(CommentDTO commentDTO) {
        int rows = commentMapper.AddNew(commentDTO);
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除评论业务,参数:{}",id);
        int i = commentMapper.deleteById(id);
        if(i==0){
            String message = "服务器繁忙,删除失败";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        log.debug("删除成功");
    }

    @Override
    public int countComment(Long id) {
        int count = commentMapper.countComment(id);
        return count;
    }

    @Override
    public List<CommentListItemVO> comments() {
        List<CommentListItemVO> comments = commentMapper.comments();
        return comments;
    }

    @Override
    public List<CommentListItemVO> comment(Long id) {
        List<CommentListItemVO> comment = commentMapper.comment(id);
        return comment;
    }

    @Override
    public List<CommentListItemVO> commentByuId(Long id) {
        return commentMapper.commentsByuId(id);
    }

    @Override
    public List<CommentListItemVO> commentByNickName(String nickName) {
        Long id = userMapper.getIdByNickName(nickName);
        return commentMapper.commentsByuId(id);
    }

    @Override
    public List<CommentListItemVO> commentByName(String name) {
        Long id = goodsMapper.getIdByName(name);
        return commentMapper.commentsBygId(id);
    }


}
